var fs = require('fs');
const { parse } = require('json2csv');
const fields = ['type', 'title', 'url', 'date', 'step'];
const opts = { fields };

const BASE_PATH = '../out/cde/20210427/';

const asList = require(BASE_PATH + 'cde-as-LIST.json');
// console.log(JSON.stringify(asList));

function append2Csv(path, value) {
  // console.log(path, value);
  fs.appendFile(path, value, 'utf8', (err) => {
    if (err) {
      // console.log('csv 写入失败', err);
    } else {
      // console.log('csv 写入成功');
    }
  });
}

function json2Csv(name, data) {
  try {
    const csv = parse(data, opts);
    // console.log(`^^^^^^^^^^^^^^^^^^^^^^^^^ json2Csv name : ${name} , csv : ${csv}`);
    append2Csv(`${BASE_PATH}${name}.csv`, csv);
  } catch (err) {
    // console.error(`^^^^^^^^^^^^^^^^^^^^^^^  json2Csv err: ${err}`);
  }
}

json2Csv('CDE_AS', asList);
